/*
 * @Descripttion:
 * @Author: cuidm
 * @Date: 2022-04-14 10:44:57
 * @LastEditors: cuidm
 * @LastEditTime: 2022-09-28 09:44:44
 */
import React, { useState, useEffect } from "react";
import { Row, Col, Card, Descriptions, Tooltip } from "antd";
import hcpUtils from '@/utils/hcpUtils';
import '../index.less'
const ResourceItem = ({history, info}) => {

    const [count, setCount] = useState(0)
    const goResourceList = (item) => {
        if(item.moduleCode === "biz") {
            history.push({
                pathname: "/resource/catalog/biz",
                state: {...item, source:'resource'}
            })
        } else {
            history.push({
                pathname: "/resource/catalog/list/" + item.cmModuleId,
                state: item
            });
        }

    }

    useEffect(() => {
        if(info) {
            let num = 0;
            hcpUtils.treeDataIteration([info], function (item) {
                if(item.definitions && item.definitions.length) {
                    num += item.definitions.length;
                }
            })
            setCount(num);
        }
    }, [info])

    const renderItem = (group, className) => {
        return (
            <>
            {
                group.children?.map((_g, _gIndex)=> {
                    return (
                        <div key={_gIndex} className="resource-item">
                            <div className="resource-item-label">
                                <Tooltip placement="top" title={_g.moduleClassificationName||'-'} style={{display: 'block', width: '100%'}}>
                                    <div className="name">{_g.moduleClassificationName||'-'}</div>
                                </Tooltip>
                            </div>
                            {
                                renderItem(_g)
                            }
                        </div>
                    )
                })
            }
            {
                group.definitions && group.definitions.length ?
                <div className={`model-box ${className}`}>
                    {
                        group.definitions.map((childItem, index) => {
                            return (
                                <div className="model-item" key={childItem.cmModuleId} onClick={() => goResourceList(childItem)}>
                                    <span className={`iconfont ${childItem.moduleIcon}`}>{childItem.module}</span>
                                    <span className="name" title={childItem.moduleName}>
                                        {childItem.moduleName}
                                        &nbsp;&nbsp;{childItem?.count || 0}
                                    </span>
                                </div>
                            )
                        })
                    }
                </div>
                : null
            }
            </>
        )

    }

    return (
        <div className="resource-box">
            <div className="title">{info.moduleClassificationName}&nbsp;&nbsp;{count}</div>
            <div className="resource-item">
                {renderItem(info, 'no-label')}
            </div>
        </div>
    )
}
export default ResourceItem;
